package com.koron.ticket.work.workPermit.service;

import cn.hutool.core.lang.Assert;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.ticket.utils.StringUtils;
import com.koron.ticket.work.workPermit.bean.WorkPermit;
import com.koron.ticket.work.workPermit.bean.WorkPermitQuery;
import com.koron.ticket.work.workPermit.mapper.WorkPermitMapper;
import com.koron.ticket.work.workflow.service.WorkFlowService;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
public class WorkPermitService {

    private EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }

    private static Date currentTime = new Date();

    @Autowired
    private WorkFlowService workFlowService;

    public void insert(SessionFactory factory, WorkPermit workPermit) {
        WorkPermitMapper workPermitMapper = factory.getMapper(WorkPermitMapper.class);
        WorkPermit single = workPermitMapper.getSingle(new WorkPermitQuery(workPermit.getWorkTicketId()));
        Assert.isFalse(single != null, "信息已过期请刷新界面");//所有票许可信息只允许有一条，（防止多开页面重复保存新建）
        workPermitMapper.insert(workPermit);
    }

    public void update(SessionFactory factory, WorkPermit workPermit) {
        WorkPermitMapper workPermitMapper = factory.getMapper(WorkPermitMapper.class);
        workPermitMapper.update(workPermit);
    }

    public void save(SessionFactory factory, WorkPermit workPermit) {
        String id = workPermit.getId();

        if (StringUtils.isEmpty(id)) {
            workPermit.setId(CodeTools.getCode32());
            workPermit.setCreateUserId(getCurrUser().getId());
            workPermit.setCreateUserName(getCurrUser().getName());
            workPermit.setCreateTime(new Date());
            this.insert(factory, workPermit);
        } else {
            workPermit.setUpdateUserId(getCurrUser().getId());
            workPermit.setUpdateUserName(getCurrUser().getName());
            workPermit.setUpdateTime(new Date());
            this.update(factory, workPermit);
        }
    }

    public void deleteById(SessionFactory factory, String id) {
        WorkPermitMapper workPermitMapper = factory.getMapper(WorkPermitMapper.class);
        workPermitMapper.deleteById(id);
    }

    public WorkPermit getSingle(SessionFactory factory, WorkPermitQuery workPermitQuery) {
        WorkPermitMapper workPermitMapper = factory.getMapper(WorkPermitMapper.class);
        return workPermitMapper.getSingle(workPermitQuery);
    }

    public void deleteByWorkTicketId(SessionFactory factory, String workTicketId) {
        WorkPermitMapper workPermitMapper = factory.getMapper(WorkPermitMapper.class);
        workPermitMapper.deleteByWorkTicketId(workTicketId);
    }

    public void insertByMap(SessionFactory factory, Map<String, String> workPermitMap, Integer status) throws ParseException {
        String id = workPermitMap.get("workPermitId");
        String workTicketId = workPermitMap.get("id");
        // 20211202: 兼容数据为空的情况
        Integer securityStatus = 0;
        if (workPermitMap.get("securityStatus") != null) {
            securityStatus = Integer.parseInt(workPermitMap.get("securityStatus"));
        }
        String securityArrangementChange = workPermitMap.get("securityArrangementChange");
        String workLocationDevice = workPermitMap.get("workLocationDevice");
        String workLocationSwitch = workPermitMap.get("workLocationSwitch");
        String attention = workPermitMap.get("attention");
        String otherNotes = workPermitMap.get("otherNotes");
        String permitType = workPermitMap.get("permitType");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        // 20211202: 兼容数据为空的情况
        Date workTime = new Date();
        if (workPermitMap.get("workTime") != null) {
            workTime = simpleDateFormat.parse(workPermitMap.get("workTime"));
        }
        String workFlowListString = workPermitMap.get("workFlowList");

        if (StringUtils.isEmpty(id)) {
            WorkPermit workPermit = new WorkPermit();
            workPermit.setId(CodeTools.getCode32());
            workPermit.setWorkTicketId(workTicketId);
            workPermit.setSecurityStatus(securityStatus);
            workPermit.setSecurityArrangementChange(securityArrangementChange);
            workPermit.setWorkLocationDevice(workLocationDevice);
            workPermit.setWorkLocationSwitch(workLocationSwitch);
            workPermit.setAttention(attention);
            workPermit.setOtherNotes(otherNotes);
            workPermit.setPermitType(permitType);
            workPermit.setWorkTime(workTime);
            workPermit.setCreateUserId(getCurrUser().getId());
            workPermit.setCreateUserName(getCurrUser().getName());
            workPermit.setCreateTime(new Date());
            this.insert(factory,workPermit);
        } else {
            WorkPermitQuery workPermitQuery = new WorkPermitQuery();
            workPermitQuery.setId(id);
            WorkPermit workPermit = this.getSingle(factory,workPermitQuery);

            workPermit.setWorkTicketId(workTicketId);
            workPermit.setSecurityStatus(securityStatus);
            workPermit.setSecurityArrangementChange(securityArrangementChange);
            workPermit.setWorkLocationDevice(workLocationDevice);
            workPermit.setWorkLocationSwitch(workLocationSwitch);
            workPermit.setAttention(attention);
            workPermit.setOtherNotes(otherNotes);
            workPermit.setPermitType(permitType);
            workPermit.setWorkTime(workTime);
            workPermit.setUpdateUserId(getCurrUser().getId());
            workPermit.setUpdateUserName(getCurrUser().getName());
            workPermit.setUpdateTime(new Date());
            this.update(factory,workPermit);
        }

        if (StringUtils.isNotEmpty(workFlowListString)) {
            workFlowService.insertByStringMap(factory, workFlowListString, status);
        }
    }

}
